package com.interview.javabasic.LRU;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2021/2/28

*/


import java.util.HashMap;
import java.util.Map;

public class LRUNodeDemo {
    //1.构造一个node节点作为数据载体
    class Node<K,V>{
        K key;
        V value;
        Node<K,V> pre;
        Node<K,V> next;
        public Node(){
            this.pre = this.next = null;
        }
        public Node(K key,V value){
            this.key = key;
            this.value = value;
            this.pre = this.next = null;
        }
    }
    //2.构造虚拟的双向双向链表，里面存放的就是node
    class DoubleLinkedList<K,V>{
        Node<K,V> head;
        Node<K,V> tail;
        public DoubleLinkedList(){
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.pre = head;
        }

        //3.添加到头
        public void addHead(Node<K,V> node){
            node.next = head.next;
            head.next.pre = node;
            node.pre =head;
            head.next = node;
        }

        //4.删除节点

        public void removeNode(Node<K,V> node){
            node.pre.next = node.next;
            node.next.pre = node.pre;
            node.pre = null;
            node.next = null;
        }
        //5.获得最后一个节点
        public Node getLast(){
            return tail.pre;
        }

    }
    private int cacheSize;
    Map<Integer,Node<Integer,Integer>> map;
    DoubleLinkedList<Integer,Integer> doubleLinkedList;

    public LRUNodeDemo(int cacheSize){
        this.cacheSize = cacheSize;
        map = new HashMap<>();
        doubleLinkedList = new DoubleLinkedList<>();
    }

    public int get(int key){
        if(!map.containsKey(key)){
            return -1;
        }
        Node<Integer, Integer> node = map.get(key);
        doubleLinkedList.removeNode(node);
        doubleLinkedList.addHead(node);
        return node.value;
    }

    public void put(int key , int value){
        if(map.containsKey(key)){
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            map.put(key,node);
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        }else{
            if(map.size() == cacheSize){
                Node last = doubleLinkedList.getLast();
                doubleLinkedList.removeNode(last);
                map.remove(last.value);
            }
            Node<Integer,Integer> node = new Node<>(key,value);
            map.put(key,node);
            doubleLinkedList.addHead(node);
        }
    }

    public static void main(String[] args) {
        LRUNodeDemo lruCacheDemo = new LRUNodeDemo(3);

        lruCacheDemo.put(1,1);
        lruCacheDemo.put(2,2);
        lruCacheDemo.put(3,3);
        System.out.println(lruCacheDemo.map.keySet());

        lruCacheDemo.put(4,1);
        System.out.println(lruCacheDemo.map.keySet());

        lruCacheDemo.put(3,1);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3,1);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3,1);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(5,1);
        System.out.println(lruCacheDemo.map.keySet());
    }
}
